<template>
  <div>
    <el-form ref="form" :model="form" label-width="80px">
      <el-form-item label="选择专业">
        <el-select v-model="form.subjectsId" placeholder="请选择">
          <el-option
            v-for="item in subjects"
            :key="item._id"
            :label="item.name"
            :value="item._id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="班级名称" style="width:360px;">
        <el-input v-model="form.name"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="addClasses">立即创建</el-button>
        <el-button>取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      form: {
        subjectsId: "",
        name: "",
      },
      subjects: [],
    };
  },
  created() {
    this.getSubjects();
  },
  methods: {
    async addClasses() {
      const res = await this.$api.classes.add(this.form);
      console.log(this.form);
      if (res.code) {
        this.$message({
          message: "新增班级成功",
          type: "success",
        });
        this.$router.push("/home/classList");
      } else {
        this.$message.error("新增班级失败");
      }
    },
    //获取专业全部数据
    async getSubjects() {
      const res = await this.$api.subjects.get();
      console.log(res);

      if (res.code) {
        this.subjects = res.data.rows;
      }
    },
  },
};
</script>

<style>
.el-input {
  width: 280px;
}
.el-select {
  width: 280px;
}</style>
